<?php
class LogDao {
	/**
	 * 获取数据
	 * 
	 * @param string $table        	
	 * @param string $type        	
	 * @param int $limit        	
	 * @param int $count        	
	 * @return array
	 */
	public static function getList($table, $type, $limit = 0, $count = 30, $where = "") {
		$_db = DB_MySQLi::getInstance ();
		$sql = "";
		$typeSql = "*";
		$limitSql = "";
		$whereSql = "";
		switch ($type) {
			case "count" :
				$typeSql = 'count(*) as count';
				break;
			case "limit" :
				$limitSql = "limit {$limit},{$count}";
				break;
			case "all" :
				break;
			default :
				return array ();
		}
		$sql = "SELECT {$typeSql} FROM log_{$table} {$where} {$limitSql}";

		$_result = $_db->getData_ASSOCS ( $sql );
		return $_result;
	}
	
	public static function writeLog($table, $fieldSql , $valueSql){
		if(empty($table) || empty($fieldSql) || empty($valueSql)){
			return false;
		}
		
		$_db = DB_MySQLi::getInstance ();
		$totle = self::getList ( $table, "count", 0, 0, $wh );
		if($totle[0]['count'] > LOGMAXCOUNT){
			$deleteCount = $totle[0]['count'] - LOGMAXCOUNT;
			$deleteSql = "DELETE FROM `log_{$table}` ORDER BY 'id' LIMIT {$deleteCount}";
			$_db->query($deleteSql);
		}
		
		$sql = "INSERT INTO `log_{$table}` ({$fieldSql}) VALUES ({$valueSql});";
		
		if($_db->query($sql)){
                    return $_db->getlastInsertId();
                }
                 return false;   
	}
	
	public static function updateLog($table,$updateSql, $whereSql){
		if(empty($updateSql)){
			return false;
		}
		$_db = DB_MySQLi::getInstance();
		$sql = "UPDATE `log_{$table}` SET {$updateSql} {$whereSql}";
		$_result = $_db->query($sql);
		return $_result;
	}
	
	public static function getImg($table ,$field,$where){
		if(empty($table) || empty($field) || empty($where)){
			return false;
		}
		$_db = DB_MySQLi::getInstance();
		$sql = "SELECT {$field} FROM log_{$table} WHERE {$where}";
		$_result = $_db->getData_ASSOCS ( $sql );
		return $_result;
	}
	
	public static function deleteLog($table, $whereSql){
		if(empty($whereSql)){
			return false;
		}
		$_db = DB_MySQLi::getInstance();
		$sql = "DELETE FROM `log_{$table}` {$whereSql} AND isExport = 1";
		$_result = $_db->query($sql);
		return $_result;
	}
}